package cn.i7baozh.alibaba.consumer.controller;

import cn.i7baozh.alibaba.consumer.service.ProviderService;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * @author zhangbaoqi532
 * @date 2024/12/27
 */
@RestController
@RefreshScope
public class ConsumerController {

    @Value("${config:false}")
    public String testInfo;

    @Resource
    private ProviderService providerService;

    @GetMapping("alibaba/consumer/get")
    public String getMsg(@Value("${config:false}") String t) {
        System.out.println(t);
        return "获得的远程配置值为:[" + testInfo + " ]" + providerService.getMsg();
    }

//    public static void main(String[] args) throws Exception {
//        String serverAddr = "localhost:8848";
//        String dataId = "alibaba-consumer";
//        String group = "DEFAULT_GROUP";
//        Properties properties = new Properties();
//        properties.put("serverAddr", serverAddr);
//        ConfigService configService = NacosFactory.createConfigService(properties);
//        String content = configService.getConfig(dataId, group, 5000);
//        System.out.println(content);
//        configService.addListener(dataId, group, new Listener() {
//            @Override
//            public void receiveConfigInfo(String configInfo) {
//                System.out.println("recieve1:" + configInfo);
//            }
//            @Override
//            public Executor getExecutor() {
//                return null;
//            }
//        });
//
//// 测试让主线程不退出，因为订阅配置是守护线程，主线程退出守护线程就会退出。 正式代码中无需下面代码
//        while (true) {
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}
